# -*- coding: utf-8 -*-
# @Author: Zach-Pineappleman
# @E-mail: 354399824@qq.com
# @Date:   2025-03-13 21:11:27
# @File_name:webcam_handler.py
# @Last Modified by:   Zach-Pineappleman
# @Last Modified time: 2025-03-13 21:12:06

# webcam_handler.py
# 摄像头画面 获取和分析

import cv2
import time
import threading
from PIL import Image
import oss2
from config import *
from utils import extract_behavior_type

class WebcamHandler:
    def __init__(self, app):
        self.app = app
        self.running = False
        self.cap = None

    def start(self):
        self.running = True
        self.cap = cv2.VideoCapture(0)
        threading.Thread(target=self.capture_frames).start()

    def capture_frames(self):
        while self.running:
            ret, frame = self.cap.read()
            if ret:
                img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
                self.app.update_camera_frame(img)
            time.sleep(0.1)

    def stop(self):
        self.running = False
        self.cap.release()